package com.itfreer.setting.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.itfreer.core.config.ConfigManager;
import com.itfreer.core.security.SecurityService;
import com.itfreer.core.security.UserInfoEntity;
import com.itfreer.metadata.version.DataVersionService;
import com.itfreer.setting.R;
import com.itfreer.ui.activity.BaseActivity;

import java.util.Map;

public class LoginActivity extends BaseActivity {

    private com.itfreer.ui.base.TextView loginBack;
    private EditText login_username;
    private EditText login_password;
    private Button btn_login;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setNavigationBarColor();

        setContentView(R.layout.activity_main_login);

        login_username = findViewById(R.id.et_login_username);
        login_password = findViewById(R.id.et_login_pwd);
        btn_login = findViewById(R.id.bt_login_submit);
        loginBack = findViewById(R.id.login_back);

        //从基础跳规划就会有数据
        Intent lastIntent = getIntent();
        if(lastIntent!=null) {
            boolean firstLogin = lastIntent.getBooleanExtra("firstLogin", false);
            if(firstLogin) loginBack.setVisibility(View.GONE);
        }

        //点击返回按钮
        loginBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setResult(0);
                finish();
            }
        });

        //点击登录按钮
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String usernameStr = login_username.getText().toString().trim();
                String passwordStr = login_password.getText().toString().trim();
                if (usernameStr.equals("") || passwordStr.equals("")) {
                    Toast.makeText(LoginActivity.this, "用户名密码不能为空", Toast.LENGTH_LONG).show();
                } else {
                    final String tenantid = ConfigManager.getTenantID();
                    SecurityService.instance.login(usernameStr, passwordStr, LoginActivity.this,
                            new SecurityService.UserListener() {
                        @Override
                        public void onSuccess(UserInfoEntity user) {
                            DataVersionService.instance.delete(tenantid);
                            btn_login.setText("登录成功，请稍后...");
                            // 更新配置
                            setResult(1);
                            finish();
                        }

                        @Override
                        public void onGetJwt() {

                        }

                        @Override
                        public void onFail() {

                        }
                    });
                }
            }
        });
    }
}
